package com.ruoyi.web.controller.basedata;

import com.ruoyi.basedata.domain.ElectricityUsersXb;
import com.ruoyi.basedata.domain.vo.UserXbImportantTemplate;
import com.ruoyi.basedata.service.IElectricityUsersXbService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 小用户档案Controller
 *
 * @author ruoyi
 * @date 2023-09-07
 */
@Deprecated
@RestController
@RequestMapping("/basedata/electricityUsersXb")
public class ElectricityUsersXbController extends BaseController {
    @Autowired
    private IElectricityUsersXbService usersXbService;

    /**
     * 查询小用户档案列表
     */
    @PreAuthorize("@ss.hasPermi('basedata:electricityUsersXb:list')")
    @GetMapping("/list")
    public TableDataInfo list(ElectricityUsersXb usersXb) {
        startPage();
        List<ElectricityUsersXb> list = usersXbService.selectUsersXbList(usersXb);
        return getDataTable(list);
    }

    /**
     * 导出小用户档案列表
     */
    @PreAuthorize("@ss.hasPermi('basedata:electricityUsersXb:export')")
    @Log(title = "小用户档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElectricityUsersXb usersXb) {
        List<ElectricityUsersXb> list = usersXbService.selectUsersXbList(usersXb);
        ExcelUtil<ElectricityUsersXb> util = new ExcelUtil<ElectricityUsersXb>(ElectricityUsersXb.class);
        util.exportExcel(response, list, "小用户档案数据");
    }

    /**
     * 获取小用户档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('basedata:electricityUsersXb:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(usersXbService.selectUsersXbByID(ID));
    }

    /**
     * 新增小用户档案
     */
    @PreAuthorize("@ss.hasPermi('basedata:electricityUsersXb:add')")
    @Log(title = "小用户档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElectricityUsersXb usersXb) {
        int i = usersXbService.insertUsersXb(usersXb);
        if (i == 0) {
            return error("该用户已存在");
        }
        return toAjax(i);
    }

    /**
     * 修改小用户档案
     */
    @PreAuthorize("@ss.hasPermi('basedata:electricityUsersXb:edit')")
    @Log(title = "小用户档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElectricityUsersXb usersXb) {
        int flag = usersXbService.updateUsersXb(usersXb);
        if (flag == 0) {
            return error("该用户编号已存在");
        }
        return toAjax(flag);
    }

    /**
     * 删除小用户档案
     */
    @PreAuthorize("@ss.hasPermi('basedata:electricityUsersXb:remove')")
    @Log(title = "小用户档案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        int flag = usersXbService.deleteUsersXbByIDs(IDs);
        if (flag == 0) {
            return error("有房间 已绑定该用户，不能删除");
        }
        return toAjax(flag);
    }

    @Log(title = "小表用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @PreAuthorize("@ss.hasPermi('basedata:electricityUsersXb:import')")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ElectricityUsersXb> util = new ExcelUtil<ElectricityUsersXb>(ElectricityUsersXb.class);
        List<ElectricityUsersXb> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = usersXbService.importUser(userList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/getImportTemplate")
    public void getImportTemplate(HttpServletResponse response) throws Exception {
        ExcelUtil<UserXbImportantTemplate> util = new ExcelUtil<UserXbImportantTemplate>(UserXbImportantTemplate.class);
        List<UserXbImportantTemplate> list = Arrays.asList(UserXbImportantTemplate.builder().build());
        util.exportExcel(response, list, "用户档案导入模版");
    }
}
